import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/me/data/api/browsing_life_history_api.dart';
import 'package:casdon/modules/me/data/api/browsing_recipe_history_api.dart';
import 'package:casdon/modules/me/data/repository/me_repository.dart';
import 'package:casdon/modules/me/data/repository/me_repository_impl.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class BrowsingLifeHistoryUsecase {
  final MeRepository _repository;

  const BrowsingLifeHistoryUsecase(this._repository);

  Future<ResponseBrowsingLifeHistoryApi> execute({
    required int pageNum,
    required int pageSize,
  }) async {
    try {
      var ret = await _repository.getLifeBrowsingHistory(
        pageNum: pageNum,
        pageSize: pageSize,
      );
      if (ret == null) {
        throw Exception("BrowsingLifeHistoryUsecase 失败, 结果为空");
      }
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("BrowsingLifeHistoryUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("BrowsingLifeHistoryUsecase 失败");
    }
  }
}

final browsingLifeHistoryUsecaseProvider = Provider(
  (ref) => BrowsingLifeHistoryUsecase(ref.read(meRepositoryProvider)),
);
